/**
 * 加载数据并 使用回调函数进行处理
 * @@param loadObj 加载对象
 * @@param callback 回调函数
 * @@param params 查询参数
 * @@param relatedObj 关联对象(与本次加载的对象有数据上的依赖关系)
 * @void
 * @ Discription:
 * @paygroupAnalysis.js
 */
function loadDataPayGroup(loadObj,callback,params,relatedObj) {
	// 定义延迟对象
	var dtd = $.Deferred();
	// 构造查询指标code
	var quotas = [] ;
	for(var o in loadObj.quotas) {
		// 跳过页面计算指标
		if(loadObj.quotas[o].code.indexOf("|") > 0) {
			continue ;
		}
		quotas.push(loadObj.quotas[o].code) ; 
	}
	// 关联对象加载指标
	if( relatedObj && relatedObj.quotas ) {
		for(var o in relatedObj.quotas) {
			quotas.push(relatedObj.quotas[o].code) ; 
		}
	}
	var quotastr = quotas.join(",") ;
	var param=drgUI.setAjaxData(tabId);
	// 设置默认值
	param.isGetCc = '0' ;
	param.monitorType = "hosp" ;
	
	param.quotaCode = quotastr ;
	// 根据传的参数修改默认值
	if(params && params.monitorType) {
		param.monitorType = params.monitorType ;
	} 
	if(params && params.mdcCode) {
		param.parmdcCode = params.mdcCode ;
	} 
	if(params && params.pardrgCode) {
		param.pardrgCode = params.pardrgCode ;
	}
	if(params && params.depCode) {
		param.pardepCode = params.depCode ;
	}
	if(params && params.compareType) {
		param.compareType = params.compareType ;
	}
	if(params && params.isGetCc) {
		param.isGetCc = params.isGetCc ;
	}
	if(params && params.msCode) {
		param.msCode = params.msCode ;
	}
	if(params && params.isPayGroup) {
		param.isPayGroup = params.isPayGroup ;
	}
	// 付费病组 只统计医保病人的
	param.pType = "1" ;
	$.post(contextPath + "/DrgsIndexCal/indexCal.dhc", param ).done(function(data) {
		var data = data.data ;
		// 过滤病组数据
		if(loadObj.id == "drgAvgFeeEch") {
			loadObj.data[params.compareType] = data ;
		} else if(params && params.monitorType ){
			// 判断请求的指标中是否含有 PayGroupCnt
			if(payGroupFilter(loadObj.quotas,"code","PayGroupCnt").length >0 ) {
				data = payGroupFilter(data,"PayGroupCnt") ;
				// 判断请求的指标中是否含有 PayGroupActualFee
			} else if(payGroupFilter(loadObj.quotas,"code","PayGroupActualFee").length >0) {
				data = payGroupFilter(data,"PayGroupActualFee") ;
			}
			loadObj.data[params.monitorType] = data ;
		} else {
			loadObj.data = data ;
		}
		if(callback && relatedObj) {
			callback(loadObj,relatedObj) ;
		} else if(callback) {
			callback(loadObj) ;
		}
		// 将延迟对象改为完成状态
		dtd.resolve(); 
	});
	// 此时的dtd对象为 未完成状态
	return dtd.promise();
}



/***
 * 从数组中过滤指定属性(code)的对象
 * @@param data
 * @@param property
 * @@param propertyVal
 * @@returns
 * @any
 * @ Discription:
 * @paygroupCommon.js
 */
function payGroupFilter(data,property,propertyVal) {
	if(propertyVal) {
		data=data.filter(function(obj){
			return obj[property] == propertyVal ;
		}); 
	} else {
		data=data.filter(function(obj){
			return obj[property] != undefined ;
		}); 
	}
	return data ;
}


/**数组根据数组对象中的某个属性值进行排序的方法 
 * 使用例子：newArray.sort(orderBy('number',false)) //表示根据number属性降序排列;若第二个参数不传递，默认表示升序排序
 * @param attr 排序的属性 如number属性
 * @param rev true表示升序排列，false降序排序
 * */
function order(attr,rev){
    //第二个参数没有传递 默认升序排列
    if(rev ==  undefined){
        rev = 1;
    }else{
        rev = (rev) ? 1 : -1;
    }
    
    return function(a,b){
        a = a[attr];
        b = b[attr];
        if(a < b){
            return rev * -1;
        }
        if(a > b){
            return rev * 1;
        }
        return 0;
    }
}
/**
 * 
 * @ Discription:费用分布
 * @Autor ：mazhaoyan
 * @param a 待比较值
 * @param b 标杆值
 * @param c 所需字段名 数组
 * void
 * paygroupCommon.js
 * 2017年8月30日下午8:18:42
 */
function costStructure(a,b,c){
	$.post(contextPath + "/pages/common/top-dialog.jsp").done(function(data) {
		var dialog  = $(data);
		dialog.find(".modal-dialog").attr("id","costStructureDialog");
		dialog.find(".modal-title").html("病组费用构成");
		dialog.modal({
			keyboard: false
		}).on({
			"hidden.bs.modal" : function(){
				$(this).remove();
			},
			"shown.bs.modal" : function(){
				var str = "<div class = 'col-md-12'>"
						 +"	<div class = 'col-md-5'>"
						 +"		<div id='costStructureChart' style='width: 100%; height: 300px;'></div>"
						 +"	</div>"
						 +"	<div class = 'col-md-7'>"
						 +"		<table id='costStructureTable' class='table'></table>"
						 +"	</div>"
						 +"</div>"
				
				dialog.find("#echartcq4").html(str);
				//医疗费用 护理费用 管理费用 医技费用 药品耗材费用 次均费用
				var rowMed = {"desc":"医疗费用 ","val1":a[c[1]],"per1":formatNumber(a[c[1]]/a[c[6]]*100,2),"val2":b[c[1]],"per2":formatNumber(b[c[1]]/b[c[6]]*100,2)};
				var rowNurs = {"desc":"护理费用","val1":a[c[2]],"per1":formatNumber(a[c[2]]/a[c[6]]*100,2),"val2":b[c[2]],"per2":formatNumber(b[c[2]]/b[c[6]]*100,2)};
				var rowManage = {"desc":"管理费用 ","val1":a[c[3]],"per1":formatNumber(a[c[3]]/a[c[6]]*100,2),"val2":b[c[3]],"per2":formatNumber(b[c[3]]/b[c[6]]*100,2)};
				var rowTech = {"desc":"医技费用 ","val1":a[c[4]],"per1":formatNumber(a[c[4]]/a[c[6]]*100,2),"val2":b[c[4]],"per2":formatNumber(b[c[4]]/b[c[6]]*100,2)};
				var rowDrug = {"desc":"药品耗材费用 ","val1":a[c[5]],"per1":formatNumber(a[c[5]]/a[c[6]]*100,2),"val2":b[c[5]],"per2":formatNumber(b[c[5]]/b[c[6]]*100,2)};
				var rowAvgFee = {"desc":"合计（次均费用）","val1":a[c[6]],"per1":"100","val2":b[c[6]],"per2":"100"};
				
				var tabledata = [];
				tabledata.push(rowMed);
				tabledata.push(rowNurs);
				tabledata.push(rowManage);
				tabledata.push(rowTech);
				tabledata.push(rowDrug);
				tabledata.push(rowAvgFee);
				var columns=[
			                  	{"data": "desc","title": "费用类别"},
			                  	{"data": "val1","title":a[c[0]],
			                  		"render": function(obj, type, full) {
				               			if(full.val1 > full.val2 )
			               				{
				               				return "<span style = 'color:red'>"+formatNumPayGroup(full.val1,2)+"</span>";
			               				}
				               			return formatNumPayGroup(full.val1,2) ;
				           			}
			                  	},
			                  	{"data": "per1","title":a[c[0]]+"占比（%）",
				           			"render": function(obj, type, full) {
				           				return formatNumPayGroup(full.per1,2) ;
				           			}
			                  	},
			                  	{"data": "val2","title":"标杆值" ,
				           			"render": function(obj, type, full) {
				           				return formatNumPayGroup(full.val2,2) ;
				           			}
			                  	},
			                  	{"data": "per2","title":"标杆占比（%）",
				           			"render": function(obj, type, full) {
				           				return formatNumPayGroup(full.per2,2) ;
				           			}
			                  	}
			                 ];
				var paramsDataTables={lengthChange:false,ordering:false,stateSave:true,info:false,paging:false,tableTitle:"付费病组维护",columnDefs:[{"orderable": false,"targets": [ 0 ] }]};
				if(a.desc == "标杆值") {
					columns.splice(1,2);
				}
				dialog.find("#echartcq4 #costStructureTable").dataTableBasic(columns,tabledata,paramsDataTables) ;
				//绘制饼图
				var costStructureChart = [];//病组类型
				var costStructureChartConfig = {"showLabel":false,"showLabelLine":false,radius:[0,'60%'],legendItemGap:5}
				//医疗费用 护理费用 管理费用 医技费用 药品耗材费用 次均费用 
				costStructureChart.push({name:'医疗费用',value:a[c[1]]});
				costStructureChart.push({name:'护理费用 ',value:a[c[2]]});
				costStructureChart.push({name:'管理费用',value:a[c[3]]});
				costStructureChart.push({name:'医技费用',value:a[c[4]]});
				costStructureChart.push({name:'药品耗材费用',value:a[c[5]]});
				pieCharts(dialog.find("#echartcq4 #costStructureChart"), costStructureChart, '费用构成图', '',costStructureChartConfig);
			}
		});
  });
}

/**
 * 付费病组数字格式化
 * @@param num
 * @@param decimal
 * @@returns
 * @any
 * @ Discription:
 * @paygroupCommon.js
 */
function formatNumPayGroup(num,decimal) {
	if(!num || isNaN(num)) {
		if(isNaN(decimal)) {
			return decimal ;
		} else {
			return formatNumber("0",decimal) ;
		}
	}
	return formatNumber(num,decimal)
}
